<?php

class PetCategory
{    
	private static $IDs=array(110101=>1,110102=>1,110201=>1,110202=>1,110203=>1,110301=>1,110302=>1,110303=>1,110401=>1,110402=>1,110403=>1,120101=>1,120102=>1,120103=>1,120201=>1,120202=>1,120301=>1,120302=>1,120303=>1,120401=>1,120402=>1,120403=>1,129901=>1,130101=>1,130102=>1,130201=>1,130202=>1,130301=>1,130302=>1,130303=>1,130401=>1,130402=>1,130403=>1,130501=>1,130502=>1,130503=>1,140101=>1,140102=>1,140103=>1,140201=>1,140202=>1,140203=>1,140301=>1,140302=>1,140401=>1,140402=>1,140403=>1,140501=>1,140502=>1,140601=>1,140602=>1,140603=>1,140604=>1,150101=>1,150102=>1,150201=>1,150202=>1,150203=>1,150301=>1,150302=>1,150303=>1,150401=>1,150402=>1,150403=>1,150501=>1,150502=>1,150503=>1,150601=>1,150602=>1,150603=>1,150604=>1,159901=>1,160101=>1,160102=>1,160103=>1,160201=>1,160202=>1,160301=>1,160302=>1,160401=>1,160402=>1,160403=>1,160501=>1,160502=>1,169901=>1,170101=>1,170102=>1,170103=>1,170201=>1,170202=>1,170203=>1,170301=>1,170302=>1,170303=>1,179901=>1,);

	public static function loadPetCategory($id)
	{
		if(!self::exists($id))
			throw new CException("unknown petCatetory[$id]");

		$classname='PetCategory_'.$id;
		return call_user_func(array($classname, 'getInstance'));
	}

	public static function exists($id)
	{
		return array_key_exists($id, self::$IDs);
	}

	public function createPet($options=array())
	{
		$pet=new Pet;
		$pet->name=$this->name;
		$pet->categoryId=$this->id;
		$pet->race=$this->race;
        
        if(isset($options['attackSkills']))
            $pet->attackSkills=$options['attackSkills']+$this->attackSkills;
        else
            $pet->attackSkills=$this->attackSkills;

        if(isset($options['helpSkills']))
            $pet->helpSkills=$options['helpSkills']+$this->helpSkills;
        else
            $pet->helpSkills = $this->helpSkills;

        foreach($pet->attackSkills as $k=>$l){
            if($l==0) unset($pet->attackSkills[$k]);
        }        
        foreach($pet->helpSkills as $k=>$l){
            if($l==0) unset($pet->helpSkills[$k]);
        }

        $pet->userName=$options['userName'];
        $pet->parentId=$options['parentId'];
        $pet->level=isset($options['level'])?$options['level']:1;
        $pet->xp=isset($options['xp'])?$options['xp']:0;
                    
		$this->createFeature($pet);
		$this->createAttribute($pet, isset($options['ratio'])? $options['ratio'] : 100);

		$pet->endurance=$pet->initEndurance;
		$pet->strength=$pet->initStrength;
		$pet->dexterity=$pet->initDexterity;
		$pet->spirit=$pet->initSpirit;

		$pet->calculateStatistic();

		if(isset($options['hp']))
            $pet->setHp($options['hp']);
        else 
            $pet->recoverHp();
			
		if(isset($options['mp']))
            $pet->setMp($options['mp']);
        else
            $pet->recoverMp();

		return $pet;
	}

    protected function createFeature(&$pet)
    {
        $keys=array_keys(Pet::$features);
        $n=count($keys);
        if(!$n)
            throw new Exception('no feature defined');
        $pet->feature=$keys[mt_rand(0,$n-1)];
    }

    protected function createAttribute(&$pet, $ratio)
    {
        //TODO: random init 4-base attributes by $petBase->award ?
        //TODO: create depending on $ratio
        $pet->initEndurance=$this->endurance;
        $pet->initStrength=$this->strength;
        $pet->initDexterity=$this->dexterity;
        $pet->initSpirit=$this->spirit;
    }
}

?>
